/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yurendao.shop.forum.web;

import com.alibaba.fastjson.JSONObject;
import com.yurendao.shop.forum.domain.Comment;
import com.yurendao.shop.forum.domain.Ncategory;
import com.yurendao.shop.forum.domain.Note;
import com.yurendao.shop.forum.exception.NoteException;
import com.yurendao.shop.forum.service.ForumService;
import com.yurendao.shop.forum.service.NoteCateService;
import com.yurendao.shop.pager.domain.Page;
import com.yurendao.shop.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author shilin
 */
@RequestMapping("/forum")
@Controller
@SessionAttributes("loginuser")
public class ForumController {

    @Autowired
    private NoteCateService noteCateService;

    @Autowired
    private ForumService forumService;

    /**
     * 发帖
     * @param note
     * @param picpath
     * @param req
     * @return
     */
    @RequestMapping(value = "/seekhelp", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject note(@ModelAttribute("note") Note note,@RequestParam(value = "picpath",required = false) String[] picpath,

                       HttpServletRequest req) {
        JSONObject jsonObject = new JSONObject();
        if(picpath==null)
            System.out.println("this is null");
        else
            System.out.println("picturesize:"+picpath.length);
        if(note.getId()!=null)
            System.out.println("noteid:"+note.getId());
        else
            System.out.println("null");
            System.out.println("picturesize:"+picpath.length);
        try {
            User user = (User) req.getSession().getAttribute("loginuser");
            if (user == null)
                throw new NoteException("40001");
            if (note.getTitle().trim() == "" || note.getContent().trim() == "" || note.getTitle() == null || note.getContent() == null)
                throw new NoteException("40002");
            note.setUid(user.getUid());
            forumService.insertNote(note);

            jsonObject.put("suceess", 200);
            return jsonObject;
        } catch (NoteException e) {
            jsonObject.put("error_code", e.getMessage());
            return jsonObject;
        }
    }


    /**
     * 帖子列表，论坛主页显示
     * @param orderByClause
     * @param pageoffset
     * @return
     */
    @RequestMapping(value = "/notelist",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject notelist(String orderByClause,int pageoffset){
        List<Note> noteList = forumService.notelist(orderByClause,pageoffset);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("notelist",noteList);
        return jsonObject;
    }

    /**
     * 删除帖子
     *
     * @param nid
     * @return
     */
    @RequestMapping(value = "/delnote", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject deleteNote(String nid, HttpServletRequest req) {
        JSONObject jsonObject = new JSONObject();
        try {
            User loginuser = (User) req.getSession().getAttribute("loginuser");
            if (loginuser == null)
                throw new NoteException("40001");
            forumService.deleteNoteByNid(nid);
            jsonObject.put("success", 201);
        } catch (NoteException e) {
            jsonObject.put("error_code", e.getMessage());
        }
        return jsonObject;
    }

    /**
     * 按关键字搜索帖子
     * http://localhost:8080/forum/ksearch?key=%E7%8C%AA&orderByClause=id&pageOffset=2
     * @param key
     * @param orderByClause
     * @param pageOffset
     * @return result
     */
    @RequestMapping(value = "/ksearch", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject findNoteByKey(String key, @RequestParam(required = false)String orderByClause, @RequestParam(required = false)Integer pageOffset) {
        if(pageOffset==null)
            pageOffset=1;
        JSONObject jsonObject = new JSONObject();
        try {
            Page<Note> datas = forumService.selectNoteByKeyWord("%"+key+"%", orderByClause,pageOffset);
            jsonObject.put("keyword", key);
            jsonObject.put("pageoffset", datas.getPageOffset());
            jsonObject.put("pagesize", datas.getTotalRecords());
            jsonObject.put("data", datas.getRecords());
            return jsonObject;
        } catch (NoteException e) {
            jsonObject.put("error_code", e.getMessage());
            return jsonObject;
        }
    }

    /**
     * 显示帖子的评论
     * http://localhost:8080/forum/shrecomm?nid=9CC972DFA2D4481F85741A46FE1A8E7B&orderByClause=noteid&pageOffset=0
     * @param nid
     * @param orderByClause
     * @return
     */
    @RequestMapping(value = "/shrecomm", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject commentDisplay(String nid, @RequestParam(required = false) String orderByClause, int pageOffset, HttpServletRequest req) {
        JSONObject jsonObject = new JSONObject();
        try {
            Page<Comment> comment = forumService.selectCommentByNote(nid, orderByClause, pageOffset);
            jsonObject.put("pagesize", 20);
            jsonObject.put("data", comment);
        } catch (NoteException e) {
            jsonObject.put("errorcode", e.getMessage());
        }
        return jsonObject;
    }

    /**
     * 显示帖子详情
     *
     * @param nid
     * @return
     */
    @RequestMapping(value = "/seekdetail", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject aCommentDisplay(String nid, HttpServletRequest req) {
        JSONObject jsonObject = new JSONObject();
        try {
            User user = (User) req.getSession().getAttribute("loginuser");
            if (user == null)
                throw new NoteException("40001");
            Note note = forumService.displayNoteById(nid);
            jsonObject.put("data", note);
        } catch (NoteException e) {
            jsonObject.put("errorcode", e.getMessage());
        }
        return jsonObject;
    }


    /**
     * 评论
     *
     * @param nid
     * @param content
     * @param req
     * @return
     */
    @RequestMapping(value = "/recomment", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject comment(String nid, String content, HttpServletRequest req) {
        JSONObject jsonObject = new JSONObject();
        Comment comment = new Comment();
        try {
            User user = (User) req.getSession().getAttribute("loginuser");
            if (user == null)
                throw new NoteException("40001");
            if (content == null)
                throw new NoteException("40002");
            if (nid != null)
                comment.setNoteid(nid);
            comment.setUid(user.getUid());
            comment.setContent(content);
            comment.setId("3");
            forumService.insertComment(comment);
            jsonObject.put("suceess", 200);
        } catch (NoteException e) {
            jsonObject.put("error_code", e.getMessage());
        }
        return jsonObject;
    }


    /**
     * 删除评论
     * @param cid
     * @param req
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject delComment(String cid, HttpServletRequest req) {
        JSONObject jsonObject = new JSONObject();
        try {
            User loginuser = (User) req.getSession().getAttribute("loginuser");
            if (loginuser == null)
                throw new NoteException("40001");
            forumService.deleteCommentByCid(cid);
            jsonObject.put("success", 201);
        } catch (NoteException e) {
            jsonObject.put("errorcode", e.getMessage());
        }
        return jsonObject;
    }

    /**
     * http://localhost:8080/forum/ncate
     * 获取分类栏
     * @return
     */
    @RequestMapping(value = "/ncate",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getAllCategorys(){
        JSONObject jsonObject = new JSONObject();
        List<Ncategory> ncategories = noteCateService.getAllCategory();
        jsonObject.put("error_code",0);
        jsonObject.put("data",ncategories);
        return jsonObject;
    }

    /**
     * http://localhost:8080/forum/cate?ncid=10
     * 分类查找帖子
     * @param ncid
     * @param orderByClause
     * @param pageOffset
     * @return
     */
    @RequestMapping(value = "/cate",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getNoteByCate(Integer ncid,@RequestParam(required = false)String orderByClause,@RequestParam(required = false)Integer pageOffset){
        JSONObject jsonObject = new JSONObject();
        if(pageOffset==null)
            pageOffset=1;
        try {
            Page<Note> datas = noteCateService.findNoteByCate(ncid, orderByClause, pageOffset);
            jsonObject.put("ncid", ncid);
            jsonObject.put("pageoffset", datas.getPageOffset());
            jsonObject.put("pagesize", datas.getTotalRecords());
            jsonObject.put("data", datas.getRecords());
            return jsonObject;
        } catch (NoteException e) {
            jsonObject.put("error_code", e.getMessage());
            return jsonObject;
        }
    }
}
